{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.8 \u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f60\u6784\u5efa\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u60f3\u5c06\u5e95\u5c42\u5f02\u5e38\u5305\u88c5\u6210\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521b\u5efa\u65b0\u7684\u5f02\u5e38\u5f88\u7b80\u5355\u2014\u2014\u5b9a\u4e49\u65b0\u7684\u7c7b\uff0c\u8ba9\u5b83\u7ee7\u627f\u81ea Exception \uff08\u6216\u8005\u662f\u4efb\u4f55\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5f02\u5e38\u7c7b\u578b\uff09\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7f16\u5199\u7f51\u7edc\u76f8\u5173\u7684\u7a0b\u5e8f\uff0c\u4f60\u53ef\u80fd\u4f1a\u5b9a\u4e49\u4e00\u4e9b\u7c7b\u4f3c\u5982\u4e0b\u7684\u5f02\u5e38\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class NetworkError(Exception):\n    pass\n\nclass HostnameError(NetworkError):\n    pass\n\nclass TimeoutError(NetworkError):\n    pass\n\nclass ProtocolError(NetworkError):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u7528\u6237\u5c31\u53ef\u4ee5\u50cf\u901a\u5e38\u90a3\u6837\u4f7f\u7528\u8fd9\u4e9b\u5f02\u5e38\u4e86\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    msg = s.recv()\nexcept TimeoutError as e:\n    ...\nexcept ProtocolError as e:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u5e94\u8be5\u603b\u662f\u7ee7\u627f\u81ea\u5185\u7f6e\u7684 Exception \u7c7b\uff0c\n\u6216\u8005\u662f\u7ee7\u627f\u81ea\u90a3\u4e9b\u672c\u8eab\u5c31\u662f\u4ece Exception \u7ee7\u627f\u800c\u6765\u7684\u7c7b\u3002\n\u5c3d\u7ba1\u6240\u6709\u7c7b\u540c\u65f6\u4e5f\u7ee7\u627f\u81ea BaseException \uff0c\u4f46\u4f60\u4e0d\u5e94\u8be5\u4f7f\u7528\u8fd9\u4e2a\u57fa\u7c7b\u6765\u5b9a\u4e49\u65b0\u7684\u5f02\u5e38\u3002\nBaseException \u662f\u4e3a\u7cfb\u7edf\u9000\u51fa\u5f02\u5e38\u800c\u4fdd\u7559\u7684\uff0c\u6bd4\u5982 KeyboardInterrupt \u6216 SystemExit\n\u4ee5\u53ca\u5176\u4ed6\u90a3\u4e9b\u4f1a\u7ed9\u5e94\u7528\u53d1\u9001\u4fe1\u53f7\u800c\u9000\u51fa\u7684\u5f02\u5e38\u3002\n\u56e0\u6b64\uff0c\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u672c\u8eab\u6ca1\u4ec0\u4e48\u610f\u4e49\u3002\n\u8fd9\u6837\u7684\u8bdd\uff0c\u5047\u5982\u4f60\u7ee7\u627f BaseException\n\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0d\u4f1a\u88ab\u6355\u83b7\u800c\u76f4\u63a5\u53d1\u9001\u4fe1\u53f7\u9000\u51fa\u7a0b\u5e8f\u8fd0\u884c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7a0b\u5e8f\u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u4f7f\u5f97\u4f60\u7684\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u80fd\u6e05\u6670\u663e\u793a\u8c01\u5e94\u8be5\u9605\u8bfb\u8fd9\u4e2a\u4ee3\u7801\u3002\n\u8fd8\u6709\u4e00\u79cd\u8bbe\u8ba1\u662f\u5c06\u81ea\u5b9a\u4e49\u5f02\u5e38\u901a\u8fc7\u7ee7\u627f\u7ec4\u5408\u8d77\u6765\u3002\u5728\u590d\u6742\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\n\u4f7f\u7528\u57fa\u7c7b\u6765\u5206\u7ec4\u5404\u79cd\u5f02\u5e38\u7c7b\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u5b83\u53ef\u4ee5\u8ba9\u7528\u6237\u6355\u83b7\u4e00\u4e2a\u8303\u56f4\u5f88\u7a84\u7684\u7279\u5b9a\u5f02\u5e38\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    s.send(msg)\nexcept ProtocolError:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u80fd\u6355\u83b7\u66f4\u5927\u8303\u56f4\u7684\u5f02\u5e38\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    s.send(msg)\nexcept NetworkError:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5b9a\u4e49\u7684\u65b0\u5f02\u5e38\u91cd\u5199\u4e86 __init__() \u65b9\u6cd5\uff0c\n\u786e\u4fdd\u4f60\u4f7f\u7528\u6240\u6709\u53c2\u6570\u8c03\u7528 Exception.__init__() \uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class CustomError(Exception):\n    def __init__(self, message, status):\n        super().__init__(message, status)\n        self.message = message\n        self.status = status"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\uff0c\u4e0d\u8fc7Exception\u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\u63a5\u53d7\u6240\u6709\u4f20\u9012\u7684\u53c2\u6570\u5e76\u5c06\u5b83\u4eec\u4ee5\u5143\u7ec4\u5f62\u5f0f\u5b58\u50a8\u5728 .args \u5c5e\u6027\u4e2d.\n\u5f88\u591a\u5176\u4ed6\u51fd\u6570\u5e93\u548c\u90e8\u5206Python\u5e93\u9ed8\u8ba4\u6240\u6709\u5f02\u5e38\u90fd\u5fc5\u987b\u6709 .args \u5c5e\u6027\uff0c\n\u56e0\u6b64\u5982\u679c\u4f60\u5ffd\u7565\u4e86\u8fd9\u4e00\u6b65\uff0c\u4f60\u4f1a\u53d1\u73b0\u6709\u4e9b\u65f6\u5019\u4f60\u5b9a\u4e49\u7684\u65b0\u5f02\u5e38\u4e0d\u4f1a\u6309\u7167\u671f\u671b\u8fd0\u884c\u3002\n\u4e3a\u4e86\u6f14\u793a .args \u7684\u4f7f\u7528\uff0c\u8003\u8651\u4e0b\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u5185\u7f6e\u7684 RuntimeError` \u5f02\u5e38\u7684\u4ea4\u4e92\u4f1a\u8bdd\uff0c\n\u6ce8\u610f\u770braise\u8bed\u53e5\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u4e2a\u6570\u662f\u600e\u6837\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    raise RuntimeError('It failed')\nexcept RuntimeError as e:\n    print(e.args)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    raise RuntimeError('It failed', 42, 'spam')\nexcept RuntimeError as e:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "    print(e.args)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u8003`Python\u5b98\u65b9\u6587\u6863 <https://docs.python.org/3/tutorial/errors.html>`_"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}